package com.authine.cloudpivot.web.api.controller;

import com.authine.cloudpivot.engine.api.model.organization.RoleModel;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.service.PortalService;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author: zyj
 * @time: 2022/2/16 13:46
 */
@RestController
@RequestMapping("/api/customPortal")
@Slf4j
public class CustomPortalController extends BaseController {

    @Autowired
    private PortalService portalService;

    @Value("${customPortal.flowPowerUrl}")
    private String flowPowerUrl;

    @Value("${customPortal.sheetPowerUrl}")
    private String sheetPowerUrl;

    @Value("${customPortal.reportUrl}")
    private String reportUrl;

    /***
     * gp --gpTrend
     * op --opTrend
     * 服务类型  --serviceType
     * 线上招聘  --recruitmentWeekDataXs
     * 线下招聘工时  --recruitmentWeekDataXxGs
     * 线下招聘人数  --recruitmentWeekDataXxRs
     * 线下招聘不确定人数  --recruitmentWeekDataXxBqdRs
     * 执行完成率  --subjectExecuteRate
     * 招聘顾问完成情况  --employmentRanking
     * 店检  --projectTaskIssue
     */
    /**
     * 首页权限
     * @param
     * @return
     */
    @ApiOperation(value = "首页权限" )
    @GetMapping("/portalPower")
    @ResponseBody
    public Map<String, Object> portalPower(){
        Map<String, Object> returnMap = new HashMap<>();
        List<Map<String, Object>> list1 = new ArrayList<>();
        List<Map<String, Object>> list2 = new ArrayList<>();
        List<Map<String, Object>> list3 = new ArrayList<>();
        List<String> list4 = new ArrayList<>();

        String userId = getUserId();
        List<String> userIds = new ArrayList<>();
        userIds.add(userId);
        Map<String, List<RoleModel>> rolesByUserIds = getOrganizationFacade().getRolesByUserIds(userIds);
        if(MapUtils.isNotEmpty(rolesByUserIds)){
            List<RoleModel> roles = rolesByUserIds.get(userId);
            if(CollectionUtils.isNotEmpty(roles)){
                List<String> roleIds = new ArrayList<>();
                for (RoleModel role : roles) {
                    roleIds.add(role.getId());
                }
                List<Map<String, String>> portalPower = portalService.getPortalPower(roleIds);

                if(CollectionUtils.isNotEmpty(portalPower)){
                    Set<String> flowPowers = new HashSet<>();
                    Set<String> sheetPowers = new HashSet<>();
                    Set<String> dataSends = new HashSet<>();
                    Set<String> reportPowers = new HashSet<>();

                    for (Map<String, String> map : portalPower) {
                        String flowPower = map.get("flowPower");
                        String sheetPower = map.get("sheetPower");
                        String dataSend = map.get("dataSend");
                        String reportPower = map.get("reportPower");
                        if(StringUtil.isNotBlank(flowPower)){
                            String[] split = flowPower.split(",");
                            for (String s : split) {
                                if(!flowPowers.contains(s)){
                                    flowPowers.add(s);
                                }
                            }
                        }
                        if(StringUtil.isNotBlank(sheetPower)){
                            String[] split = sheetPower.split(",");
                            for (String s : split) {
                                if(!sheetPowers.contains(s)){
                                    sheetPowers.add(s);
                                }
                            }
                        }
                        if(StringUtil.isNotBlank(dataSend)){
                            String[] split = dataSend.split(",");
                            for (String s : split) {
                                if(!dataSends.contains(s)){
                                    dataSends.add(s);
                                }
                            }
                        }
                        if(StringUtil.isNotBlank(reportPower)){
                            String[] split = reportPower.split(",");
                            for (String s : split) {
                                if(!reportPowers.contains(s)){
                                    reportPowers.add(s);
                                }
                            }
                        }
                    }

                    //流程
                    for (String flowPower : flowPowers) {
                        String workflowName = portalService.getWorkflowName(flowPower);
                        Map<String, Object> map = new HashMap<>();
                        map.put("name",workflowName);
                        String url = flowPowerUrl;
                        url = url.replaceAll("C_O_D_E",flowPower);
                        map.put("url",url);
                        list1.add(map);
                    }
                    //项目平台
                    for (String sheetPower : sheetPowers) {
                        Map<String, String> parentMap = portalService.getParentId(sheetPower);
                        String name = parentMap.get("name");
                        String parentId = parentMap.get("parentId");
                        Map<String, Object> map = new HashMap<>();
                        map.put("name",name);
                        String url = sheetPowerUrl;
                        url = url.replaceAll("P_A_R_E_N_T_I_D",parentId);
                        url = url.replaceAll("C_O_D_E",sheetPower);
                        map.put("url",url);
                        list2.add(map);
                    }
                    //数据上报
                    for (String dataSend : dataSends) {
                        Map<String, String> parentMap = portalService.getParentId(dataSend);
                        String name = parentMap.get("name");
                        String parentId = parentMap.get("parentId");
                        Map<String, Object> map = new HashMap<>();
                        map.put("name",name);
                        String url = sheetPowerUrl;
                        url = url.replaceAll("P_A_R_E_N_T_I_D",parentId);
                        url = url.replaceAll("C_O_D_E",dataSend);
                        map.put("url",url);
                        list3.add(map);
                    }
                    //报表
                    for (String reportPower : reportPowers) {
                        list4.add(reportPower);
                    }
                }
            }
        }
        returnMap.put("flowPower",list1);
        returnMap.put("sheetPower",list2);
        returnMap.put("dataSend",list3);
        returnMap.put("reportPower",list4);
        returnMap.put("reportUrl",reportUrl);
        return returnMap;
    }

    /**
     * 发起流程按钮权限
     * @param
     * @return
     */
    @ApiOperation(value = "发起流程按钮权限" )
    @GetMapping("/workFlowPower")
    @ResponseBody
    public boolean workFlowPower() {
        boolean returnValue = false;
        String userId = getUserId();
        List<String> userIds = new ArrayList<>();
        userIds.add(userId);
        Map<String, List<RoleModel>> rolesByUserIds = getOrganizationFacade().getRolesByUserIds(userIds);
        if (MapUtils.isNotEmpty(rolesByUserIds)) {
            List<RoleModel> roles = rolesByUserIds.get(userId);
            if (CollectionUtils.isNotEmpty(roles)) {
                List<String> roleIds = new ArrayList<>();
                for (RoleModel role : roles) {
                    roleIds.add(role.getId());
                }
                List<String> powers = portalService.getWorkFlowPower(roleIds);
                for (String power : powers) {
                    if ("是".equals(power)) {
                        returnValue = true;
                        break;
                    }
                }
            }
        }
        return returnValue;
    }
}
